package com.jiang.admin.core.trigger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 任务池
 * @author jiangyougan 2018年12月2日 下午5:56:00 
 *
 */
public class JobTriggerPoolHelper {
	
    private static Logger logger = LoggerFactory.getLogger(JobTriggerPoolHelper.class);
    
    /**
     * 	单例对象，负责触发
     */
    private static JobTriggerPoolHelper helper = new JobTriggerPoolHelper();

    public static void trigger(int jobId, String executorParam) {
        helper.addTrigger(jobId, executorParam);
    }
    
    public static void toStop() {
        helper.stop();
    }

    /**
     * 	线程池：用于异步触发任务
     *  Quartz定时任务默认都是并发执行的，不会等待上一次任务执行完毕，只要间隔时间到就会执行, 如果定时任执行太长，会长时间占用资源，导致其它任务堵塞。
     */
    private ThreadPoolExecutor triggerPool = new ThreadPoolExecutor(
            32,
            256,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(1000));


    public void addTrigger(final int jobId, final String executorParam) {
        triggerPool.execute(new Runnable() {
            @Override
            public void run() {
                JobTrigger.trigger(jobId, executorParam);
            }
        });
    }

    public void stop() {
        //triggerPool.shutdown();
        triggerPool.shutdownNow();
        logger.info(">>>>>>>>> job trigger thread pool shutdown success.");
    }

}
